package com.zhaopin.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lietou1986 on 2017/2/6.
 */
public class HttpUtils {



    public static String get(String url) {
        return get(url, 2000);
    }

    public static String get(String url, int time) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        try {
            HttpGet httpGet = new HttpGet(url);
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(time).setConnectTimeout(time).build();
            httpGet.setConfig(requestConfig);
            response = httpclient.execute(httpGet);
            int scode = response.getStatusLine().getStatusCode();
            if (scode == HttpStatus.SC_OK) {
                HttpEntity entity = response.getEntity();
                String returnStr = EntityUtils.toString(entity);
//                logger.info("http get OK" + ":" + returnStr);
                return returnStr;
            }
        } catch (Exception e) {
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                if (httpclient != null) {
                    httpclient.close();
                }
            } catch (Exception e) {
            }
        }
        return null;
    }

    public static String postParam(String urlStr, String param) {
        return postParam(urlStr, param, 2000);
    }

    public static String postParam(String urlStr, String param, int time) {

        if (urlStr == null) {
            return null;
        }
        if (urlStr.indexOf("http://") == -1) {
            urlStr = "http://" + urlStr;
        }
        URL url;
        HttpURLConnection con = null;
        StringBuffer result = new StringBuffer();
        try {
            url = new URL(urlStr);
            con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");
            con.setDoOutput(true);
            con.setConnectTimeout(time);

            con.getOutputStream().write(param.getBytes());
            con.getOutputStream().flush();
            con.getOutputStream().close();
            InputStream in = con.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(in));
            String line;
            while ((line = rd.readLine()) != null) {
                result.append(line + System.getProperty("line.separator"));
            }

        } catch (Exception e) {
        } finally {
            if (con != null) {
                con.disconnect();
            }
        }
        return result.toString();
    }
}
